package com.mcf.xiaolinju.service.impl;

import com.mcf.xiaolinju.entity.Goods;
import com.mcf.xiaolinju.entity.UserDetails;
import com.mcf.xiaolinju.mapper.GoodsMapper;
import com.mcf.xiaolinju.mapper.UserDetailsMapper;
import com.mcf.xiaolinju.mapper.UserMapper;
import com.mcf.xiaolinju.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcf.xiaolinju.vo.GoodsListVO;
import com.mcf.xiaolinju.vo.GoodsVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcf
 * @since 2022-02-26
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
@Resource
GoodsMapper goodsMapper;
@Resource
    UserMapper userMapper;
@Resource
    UserDetailsMapper userDetailsMapper;
    public String insertgoods(int userid, int villageid, int classifyid, String name, String brand, String type, String extent, String channel, String content, String price, String priced, String count, String buytime, String pictureurl) {
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

        if(buytime !="") {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            if(goodsMapper.insertgoods(userid,villageid,classifyid,name,brand,type,extent,channel,content,Double.parseDouble(price),Double.parseDouble(priced),Integer.parseInt(count),LocalDate.parse(buytime, fmt),pictureurl,ldt)>0){
                return "发布成功，需等待管理员审核";
            }
            else{
                return "发布失败";
            }
        }else{
            LocalDate datetime=null;
            if(goodsMapper.insertgoods(userid,villageid,classifyid,name,brand,type,extent,channel,content,Double.parseDouble(price),Double.parseDouble(priced),Integer.parseInt(count),datetime,pictureurl,ldt)>0){
                return "发布成功，需等待管理员审核";
            }
            else{
                return "发布失败";
            }

        }

    }

    public List<GoodsVO> selectbyclassfyid(int classfyid,int villageid) {
        List<Goods> goods =goodsMapper.selectbyclassfyid(classfyid,villageid);
        List<GoodsVO> goodsVOS=new ArrayList<>();
        for(Goods gods:goods){
            GoodsVO goodsVO=new GoodsVO();
            goodsVO.setId(gods.getId()) ;
            goodsVO.setUsername(userMapper.getUserName(gods.getUserid()));
            goodsVO.setVillageid(gods.getVillageid());
            goodsVO.setClassifyid(gods.getClassifyid());
            goodsVO.setName(gods.getName());
            goodsVO.setCount(gods.getCount());
            goodsVO.setBrand(gods.getBrand());
            goodsVO.setType(gods.getType());
            goodsVO.setExtent(gods.getExtent());
            List<String> stings= Arrays.asList(gods.getPictureurl().split(";"));
            goodsVO.setPicture(stings.get(0));
            goodsVO.setChannel(gods.getChannel());
            goodsVO.setContent(gods.getContent());
            goodsVO.setBuytime(gods.getBuytime());
            goodsVO.setPrice(String.format("%.1f",gods.getPrice()));
            goodsVO.setPriced(String.format("%.1f",gods.getPriced()));
            goodsVO.setCreatetime(gods.getCreatetime());
            UserDetails userDetails=userDetailsMapper.selectdetailmessage(gods.getUserid());
            goodsVO.setPortrait(userDetails.getPortrait());
            goodsVOS.add(goodsVO);
        }

        return goodsVOS;
    }

    public GoodsListVO selectbyid(int id, int villageid) {
        GoodsListVO goodsListVO=new GoodsListVO();
        Goods goods= goodsMapper.selectbyid(id,villageid);
        goodsListVO.setId(goods.getId()) ;
        goodsListVO.setUsername(userMapper.getUserName(goods.getUserid()));
        goodsListVO.setVillageid(goods.getVillageid());
        goodsListVO.setClassifyid(goods.getClassifyid());
        goodsListVO.setName(goods.getName());
        goodsListVO.setCount(goods.getCount());
        goodsListVO.setBrand(goods.getBrand());
        goodsListVO.setType(goods.getType());
        goodsListVO.setExtent(goods.getExtent());
        goodsListVO.setPicture(Arrays.asList(goods.getPictureurl().split(";")));
        goodsListVO.setChannel(goods.getChannel());
        goodsListVO.setContent(goods.getContent());
        goodsListVO.setBuytime(goods.getBuytime());
        goodsListVO.setPrice(String.format("%.1f",goods.getPrice()));
        goodsListVO.setPriced(String.format("%.1f",goods.getPriced()));
        goodsListVO.setCreatetime(goods.getCreatetime());
        UserDetails userDetails=userDetailsMapper.selectdetailmessage(goods.getUserid());
        goodsListVO.setPortrait(userDetails.getPortrait());
        goodsListVO.setPhonenum(userDetailsMapper.selectphonebyid(goods.getUserid()));
        return  goodsListVO;
    }
}
